package com.xianyu.game.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xianyu.game.Mapper.UserMapper;
import com.xianyu.game.Pojo.Game;
import com.xianyu.game.Pojo.IdIntegrates;
import com.xianyu.game.Pojo.User;
import com.xianyu.game.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper um;

    /**
     * 登录
     * @param user
     * @return
     */
    @Override
    public boolean login(User user) {
        String Password = um.selectPasswordByUsername(user);
        if(Password !=null && Password.equals(user.getPassword())){
            return true;
        }else{
            return false;
        }
    }


    /**
     * 根据用户名判断用户是否存在
     * @param user
     * @return
     */
    @Override
    public boolean isExist(User user) {
        String Password = um.selectPasswordByUsername(user);
        if(Password != null){
            return true;
        }else{
            return false;
        }
    }


    /**
     * 注册
     * @param user
     */
    @Override
    public void registered(User user) {
        LocalDateTime currentDateTime = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedDateTime = currentDateTime.format(formatter);
        System.out.println(formattedDateTime);
        user.setAgeisteationDate(formattedDateTime);
        um.registered(user);
    }

    /**
     * 获取用户信息
     * @param userId
     * @return
     */
    @Override
    public User getUser(String userId) {
         return um.getUser(userId);
    }

    /***
     * 修改用户信息
     * @param user
     */
    public void revise(User user) {
        UpdateWrapper updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id",user.getUserId());
        um.update(user,updateWrapper);
    }

    /**
     * 购买游戏
     * @param idIntegrates
     */
    @Override
    public void buyGame(IdIntegrates idIntegrates) {
        um.buyGame(idIntegrates);
    }

    /**
     * 查询游戏是否已购买
     * @param idIntegrates
     * @return
     */
    @Override
    public boolean isBuy(IdIntegrates idIntegrates) {
        Integer gameId = um.isBuy(idIntegrates);
        if (gameId == null) {
            return false;
        }else {
            return true;
        }
    }

    /**
     * 查询该游戏是否已经收藏
     * @param idIntegrates
     * @return
     */
    @Override
    public boolean isCollection(IdIntegrates idIntegrates) {
        Integer gameId = um.isCollection(idIntegrates);
        if (gameId == null){
            return false;
        }else {
            return true;
        }
    }

    /**
     * 收藏游戏
     * @param idIntegrates
     */
    @Override
    public void collectionGame(IdIntegrates idIntegrates) {
        um.collectionGame(idIntegrates);
    }

    /**
     * 获取库游戏
     * @param userId
     * @return
     */
    @Override
    public List<Game> getGame(Integer userId) {
        return um.getGame(userId);
    }

    /**
     * //获取收藏的游戏
     * @param userId
     * @return
     */
    @Override
    public List<Game> getCollection(Integer userId) {
        return um.getCollection(userId);
    }

    /**
     * 是否存在购物车
     * @param idIntegrates
     * @return
     */
    @Override
    public boolean isExisCart(IdIntegrates idIntegrates) {
        if(um.isExisCart(idIntegrates) != null){
            return true;
        }else {
            return false;
        }
    }


    /**
     * 添加购物车
     * @param idIntegrates
     */
    public void addCart(IdIntegrates idIntegrates) {
        um.addCart(idIntegrates);
    }
}
